﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Platform.ObjectModel.Design;

namespace Platform.ObjectModel.Rules.Texts
{
	/// <summary>
	/// Verifica se a coleção está dentro do intervalo
	/// </summary>
	public class Range : RuleBase
	{
		private static readonly DataObjectName Name = DataObjectName.From( typeof( Range ) );

		public Range()
			: this( 0, LibraryUtility.Limits.MaxFieldValueSize )
		{ }

		public Range( int max )
			: this( 0, max )
		{ }

		public Range( int min, int max )
			: base( Name )
		{
			this.Minimum = min;
			this.Maximum = max;
		}

		public int Minimum
		{ get; set; }

		public int Maximum
		{ get; set; }

		public override bool Validate( dynamic value, DocumentContext context )
		{
			int count = 0;

			if ( value != null && value is IEnumerable )
				count = ( ( IEnumerable<object> )value ).Count();

			// Garante que não há valores acima do permitido
			int min = Math.Min( this.Minimum, LibraryUtility.Limits.MaxFieldValueSize );
			int max = Math.Min( this.Maximum, LibraryUtility.Limits.MaxFieldValueSize );

			return min <= count && count <= max;
		}
	}
}
